import { get, post, del } from '@/utils/request.js';
import { encrypt } from '@/utils/jsencrypt.js';
import { APP_CODE as appCode, BASE_URL as baseURL, DEVICE_TYPE } from '../../app.json';
import { Alert } from 'react-native';
const deviceType = DEVICE_TYPE || 'MOBILE';
// const deviceType = global.$config.DEVICE_TYPE || 'MOBILE';
// const appCode = global.$config.APP_CODE;
// 获取登录配置
export function getLoginConfig() {
  return get(`/api/public-config/platform/platformConfig/selectByConfigKey?key=defaultLogin`);
}
export function login(data: any) {
  if (data.password) {
    data.password = encrypt(data.password);
  }
  let formData = '';
  formData = formData + `username=${data['username']}`;
  formData = formData + `&password=${data['password']}`;
  if (data?.device_ip) {
    formData = formData + `&device_ip=${data['device_ip']}`;
    formData = formData + `&appCode=${appCode}`;
  }
  return post(
    `/plat/auth/oauth/token?grant_type=password&device_type=${deviceType}`,
    formData,
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Authorization: 'Basic aDNjOmgzYw==',
      },
    },
  );
}

export function loginDomain(data: any) {
  return post(
    `/api/usercenter/public/ldap/login`,
    data,
  );
}

// 获取用户信息
export function getUserInfo() {
  return get('/api/usercenter/user/info');
}

// 获取菜单
export function getAllResourceTree(params: any) {
  return get('/api/common/app/resource/getAllResourceTree', params);
}

// 登出
export function logout(accessToken: string) {
  return post({
    url: '/auth/token/logout',
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization: `Bearer ${accessToken}`,
    },
  });
}

// 修改密码
export function changePwdByOld(data: any) {
  return post('/plat/usercenter/account/changePwdByOldPwd', data);
}

// 忘记密码
export function forgetPassword(data: any) {
  data.codeType = '变更密码';
  return post('/plat/usercenter/public/forgetPassword', data);
}

// 发送验证码
// type --注册账号  变更密码
export function sendByEmail(data: any) {
  data.type = data.type || '注册账号';
  return post('/plat/usercenter/public/sendByEmail', data, {
    headers: {
      Authorization: 'Basic aDNjOmgzYw==',
    },
  });
}
// 获取手机验证码
export function sendByPhone(data: any) {
  return post('/api/usercenter/public/sendByPhone', data, {
    headers: {
      Authorization: 'Basic aDNjOmgzYw==',
    },
  });
}

// 邮箱是否存在
export function isEmailExist(data: any) {
  return post('/plat/usercenter/public/exist', data);
}

export function register(data: any) {
  data.codeType = '注册账号';
  if (data.password) {
    data.password = encrypt(data.password);
  }
  return post('/plat/usercenter/public/register', data, {
    headers: {
      Authorization: 'Basic aDNjOmgzYw==',
    },
  });
}

// 获取可供升级的安装包apk的版本
export function getLatestAppApkVersion(data: any) {
  return post('/api/common/app/v1/package/getLatestAppVersion', data);
}
// 获取可供升级的安装包apk的下载地址
export function getLatestAppApkDownloadUrl(data: any) {
  return post('/api/common/app/v1/package/getDownloadAppUrl', data);
}

// 获取消息列表
export function getAndroidMsgList(data: any) {
  return post('/api/notice/v1/android/list', data);
}

// 更新消息状态 --- 已读
export function updateAndroidMsgState(data: any) {
  return post('/api/notice/v1/android/update', data);
}

// 查询用户多端登录配置

export function queryUserMultiLoginConfig(params: any) {
  return get('/api/usercenter/loginDevice/queryUserConfig', params);
}

export function getFileDownloadUrl(params: any) {
  return get('/api/minio/api/v1/file/download', params);
}

// 获取APP设置的多语言
export function getAppLocaleList() {
  return get('/api/common/language/state/list');
}

// 获取流程审批 列表
export function getProcessData(params) {
  return get('/api/flow/definition/published/list', params);
}


export function getProcessTaskPageList(data) {
  return post('/api/flow/process/engine/task/list', data);
}

export function getProcessInstancePageList(data) {
  return post('/api/flow/instance/list', data);
}

export function getProcessDonePageList(data) {
  return post('/api/flow/instance/approve/list', data);
}

// 查询任务详情
export function getProcessDetailTask(taskId) {
  return get(`/api/flow/process/engine/detailByTask/${taskId}`);
}

export function getProcessDetailInstance(taskId) {
  return get(`/api/flow/process/engine/instance/${taskId}`);
}


export function addProcessTask(taskId) {
  return get(`/api/flow/definition/${taskId}`);
}

export function getProcessTotal(data) {
  return post(`/api/flow/process/engine/task/count`, data);
}


export function deleteProcessDraft(taskId) {
  return del(`/api/flow/instance/${taskId}`);
}


export function getProcessInstanceApproveRecord(taskId) {
  return get(`/api/flow/instance/approve/record/${taskId}`);
}
